<template>
	<view>
		<view>
			<view>
				<view class="header"><image src="../../static/image/wx_login.png"></image></view>
				<view class="content">
					<view>申请获取以下权限</view>
					<text>获得你的公开信息(昵称，头像、地区等)</text>
				</view>

				<button class="bottom" type="primary" withCredentials="true" lang="zh_CN" @click="showLoginPop">授权登录</button>

				<button class="bottom" type="primary" withCredentials="true" lang="zh_CN" @click="quxiao">暂不登录</button>
			</view>
		</view>
	</view>
</template>
<script>
import {} from 'vuex';
export default {
	// computed: {
	//           ...mapState(['userInfo'])
	//       },
	data() {
		return {};
	},
	onLoad() {
		// 获取用户信息
		var _self = this;
		_self.$uniApi.checkPhone('');
	},
	methods: {
		async showLoginPop() {
			// 有昵称的话，不用授权
			const trueLogin = uni.getStorageSync('username');
			if (trueLogin !== '') {
				// 气泡提示
				uni.showToast({
					title: '已授权',
					icon: 'none',
					duration: 2000
				});
				return false;
			}
			// 允许授权,得到用户头像昵称
			const userInfo = await this.promiseGetUserProfile();
			// 得到用户信息，先展示，在处理后端注册
			
			userInfo['uuid'] = uni.getStorageSync('openid');
			userInfo['avatarUrl'] = userInfo.avatarUrl;
			// 请求后端发起注册或登录
			const ret = await this.promiseRegister(userInfo);
			
			// 取数据并缓存
			uni.setStorageSync('user_id', ret.data.msg.id);
			uni.setStorageSync('openid', ret.data.msg.uuid);
			uni.setStorageSync('username', ret.data.msg.username);
			uni.setStorageSync('userimg', ret.data.msg.userimg);
			uni.setStorageSync('jifen', ret.data.msg.jifen);
			uni.setStorageSync('viptime', ret.data.msg.viptime);
			uni.setStorageSync('buy', ret.data.msg.buy);
			uni.setStorageSync('collect', ret.data.msg.collect);
			
			
			uni.switchTab({
				url: '../index/index'
			});
		},
		// 用户授权登录
		promiseGetUserProfile() {
			return new Promise((resolve, reject) => {
				uni.getUserProfile({
					desc: '用于完善会员资料',
					lang: 'zh_CN',
					success: user => {
						// 得到用户信息
						resolve(user.userInfo);
					},
					fail: err => {
						reject();
					}
				});
			});
		},
		// 注册
		promiseRegister(userInfo) {
			return new Promise((resolve, reject) => {
				uni.request({
					url: this.$serverUrl + '/App/Zm/oneRegister',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					method: 'POST',
					data: userInfo,
					success: ret => {
						if (ret.statusCode !== 200) {
							console.error('请求失败', ret);
							reject(ret);
						}
		
						if (ret.data.code == 1) {
							resolve(ret);
						}
					}
				});
			});
		},
		getUserInfo() {
			uni.login({
				provider: 'weixin',
				success: res => {
					if (res.errMsg == 'login:ok') {
						this.code = res.code;
						uni.getUserInfo({
							provider: 'weixin',
							success: function(infoRes) {
								//console.log(infoRes.userInfo.nickName);

								uni.request({
									url: this.$serverUrl + '/App/Zm/oneRegister',
									header: {
										'content-type': 'application/x-www-form-urlencoded'
									},
									method: 'POST',
									data: {
										uuid: res.code,
										avatarUrl: infoRes.userInfo.avatarUrl,
										nickName: infoRes.userInfo.nickName
									},
									success: ret => {
										console.log(JSON.stringify(ret.data));
										if (ret.statusCode !== 200) {
											console.log('请求失败', ret);
											return;
										}
										if (ret.data.code == 1) {
											// 取数据并赋值

											uni.setStorageSync('user_id', ret.data.msg.id);
											uni.setStorageSync('username', ret.data.msg.username);
											uni.setStorageSync('userimg', ret.data.msg.userimg);
											uni.setStorageSync('jifen', ret.data.msg.jifen);
											uni.setStorageSync('viptime', ret.data.msg.viptime);
											uni.setStorageSync('buy', ret.data.msg.buy);
											uni.setStorageSync('collect', ret.data.msg.collect);
											// 关闭动画
											uni.hideLoading();
											// 跳转到首页
											uni.reLaunch({
												url: '../index/index'
											});
										} else {
											// 关闭动画

											// 气泡提示
											uni.showToast({
												title: ret.data.msg,
												icon: 'none',
												duration: 2000
											});
										}
									}
								});
							},
							fail: () => {
								uni.showToast({ title: '获取用户信息失败', icon: 'none' });
							}
						});
					} else {
						uni.showToast({
							title: '系统异常，请联系管理员!'
						});
					}
				}
			});
		},
		quxiao() {
			uni.reLaunch({
				url: '../index/index'
			});
		}
	}
};
</script>
<style>
.header {
	margin: 90rpx 0 90rpx 50rpx;
	border-bottom: 1px solid #ccc;
	text-align: center;
	width: 650rpx;
	height: 300rpx;
	line-height: 450rpx;
}

.header image {
	width: 200rpx;
	height: 200rpx;
}

.content {
	margin-left: 50rpx;
	margin-bottom: 90rpx;
}

.content text {
	display: block;
	color: #9d9d9d;
	margin-top: 40rpx;
}

.bottom {
	border-radius: 80rpx;
	margin: 70rpx 50rpx;
	font-size: 35rpx;
}
</style>
